package round88;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * User: Samuel
 * Date: 09-23-11
 * Time: 09:03 AM
 */
public class A {
    public static void main(String[] args) throws IOException {
        new A().run();
    }

    private int m;

    private void run() throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String[] split = in.readLine().split(" ");
        int n = Integer.parseInt(split[0]);
        m = Integer.parseInt(split[1]);
        int s[] = new int[n], f[] = new int[n], t[] = new int[n];
        for (int i = 0; i < n; i++) {
            split = in.readLine().split(" ");
            s[i] = Integer.parseInt(split[0]);
            f[i] = Integer.parseInt(split[1]);
            t[i] = Integer.parseInt(split[2]);
        }
        for (int i = 0; i < n; i++) {
            System.out.println(solve(s[i], f[i], t[i]));
        }
    }

    private int solve(int s, int f, int t) {
        if (s == f) return t;
        int p = t / (m - 1);
        s--;
        f--;
        int floor = t % (m - 1);

        if (p % 2 == 0) {
            if (s < f) {
                if (s < floor) {
                    return (p + 2) * (m - 1) + f;
                } else {
                    return p * (m - 1) + f;
                }
            } else {
                if (s < floor) {
                    return (p + 2) * (m - 1) - f;
                } else {
                    return (p + 2) * (m - 1) - f;
                }
            }
        } else {
            floor = m - 1 - floor;
            if (s < f) {
                if (s <= floor) {
                    return (p + 1) * (m - 1) + f;
                } else {
                    return (p + 1) * (m - 1) + f;
                }
            } else {
                if (s <= floor) {
                    return (p + 1) * (m - 1) - f;
                } else {
                    return (p + 3) * (m - 1) - f;
                }
            }
        }
    }
}
